
//-------------------------------------------------------
// Header Files
//-------------------------------------------------------
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // for EXIT_FAILURE
#include "storage.h" // Defines max len for inputs
#include "utils.h"
#include <sys/time.h>

//-------------------------------------------------------
// Constants
//-------------------------------------------------------
#define LOGGING 2
enum {Connect = 1, Authenticate = 2, Get = 3, Set = 4, Disconnect = 5, Exit = 6, Query = 7};
char inputstr[] ="Please input the";
		int notMain = 1; // Only parent process have this as 1. 
		int firstTime =1; 
//-------------------------------------------------------
// Main
//-------------------------------------------------------
/**
 * Main function initializes the client
 * and deals with parsing user input
*/
int main(int argc, char *argv[])
{
    fileClient = stdout;
    // Variables (add 1 for '/0' character)
    int choice = 0;

    int i = 0;
    int status;
    void *conn;
    struct storage_record r; // the structure type for the record value
    // start executing 
char hostname[MAX_HOST_LEN+1] = "localhost"; 
int portNum = 1376;
    conn = storage_connect(hostname, portNum);
    if(!conn)
    {
	 sprintf(message, "Cannot connect to server @ %s:%d. Error code: %d.\n", hostname, portNum, errno);
	 logger(fileClient, message);
	 return -1;
    }
    else
    {
	printf("			1 SUCCESS\n");
    }
char username[MAX_USERNAME_LEN+1] = "admin";
char password[MAX_ENC_PASSWORD_LEN+1] = "dog4sale";

    status = storage_auth(username, password, conn); if(status == 0) printf("			2 SUCCESS\n"); else printf("2 status is %d\n", status); 


char table[MAX_TABLE_LEN+1] = "census";
char key[MAX_KEY_LEN+1] = "Anything";

    strcpy(r.value, "a abc, b 2, c 3"); 

    status = storage_set(table, key, &r, conn); if(status == 0) printf("			3 SUCCESS\n"); else printf("3 status is %d\n", status);

    status = storage_get(table, key, &r, conn); if(status == 0) printf("			4 SUCCESS\n"); else printf("4 status is %d\n", status); 

int keysMax = 100000;
char** keys;
keys = (char**) malloc(sizeof(char**) * keysMax);	// Initialize an array to store each column name
for(i = 0; i < keysMax; i++)
{
    keys[i] = (char*) malloc(sizeof(char*) * (MAX_COLNAME_LEN+1));
}
char predicates[500] = "b > 1";
    status = storage_query(table, predicates, keys, keysMax, conn); if(status != -1) printf("			5 SUCCESS with value %d\n", status); else printf("5 status is %d\n", status); 
		for(i = 0; i < status; i++)
		{
			printf("%s\n",keys[i]);
		}
		
		for(i = 0; i < keysMax; i++)	//	Free colName
		{
			free(keys[i]);
		}
		free(keys);
    status = storage_set(table, key, NULL, conn); if(status == 0) printf("			6 SUCCESS\n"); else printf("6 status is %d\n", status); 


    status = storage_disconnect(conn); if(status == 0) printf("			1000 SUCCESS\n"); else printf("1000 status is %d\n", status); 
//-----------------
while(1); 
//-----------------
    // Exit the client
    return 0;
}
